var async = require('async')
var myService = rootRequire('services/myService')
var loginUtil = rootRequire('utils/login')



module.exports =  function(server){


    server.route({
        method:'POST',path:'/register',
        handler: function(req,reply){
            var info = req.payload
            var username = info.username,
                password = info.password,
                yzm = info.yzm,
                phone=info.phone,
                repassword = info.repassword;
            const phoneReg=/^1\d{10}$/
            if(!username){
                return reply({error:'用户名不能为空'})
            }
            if(!phoneReg.test(phone)){
                return reply({error:'手机号格式有问题'})
            }
            if(password.length<6){
                return reply({error:'用户名不能少于6位'})
            }
            if(password!==repassword){
                return reply({error:"两次密码不一致"})
            }
            var hotelNo = info.hotelno || req.query['hn'] ;

            myService.register({
                hotelno:hotelNo,
                userName:username,
                userPassword:password,
                code:yzm,
                userPhone:phone,
            },function(err,body){
                if(err){
                    console.log(err)
                    return reply({error:'注册失败',原因:err})
                }
                var name = body.webmember.wmMemName
                var token = loginUtil.setJwt({hotelno:hotelNo,phone:phone,name:name})
                reply.redirect('/my?hn='+hotelNo).state('__j',token)
            })
        }
    })


    server.route({
        method:"get",path:'/logout',
        handler:function(req,reply){
            var hotelNo = req.query['hn'] ;

            reply.redirect('/my?hn='+hotelNo).state('__j',null)
        }
    })

    server.route({
        method:'POST',path:'/login',
        handler:function(req,reply){
            var info = req.payload

            var phone=info.phone,
                password = info.password

            var hotelNo = info.hotelno || req.query['hn'] ;
            myService.login({
                hotelno:hotelNo,
                userPassword:password,
                userName:phone
            },function(err,body){
                if(err){
                    return reply({
                        error:'登录失败',
                        reason:err
                    })
                }
                console.dir(body)
                var name = body.webmember.wmMemName

                var token = loginUtil.setJwt({hotelno:hotelNo,phone:phone,name:name})
                reply.redirect('/my?hn='+hotelNo).state('__j',token)
            })
        }
    })

    server.route({
        method:'GET',path:'/yzm',
        handler:function(req,reply){
            var phone = req.query['number'],
                hotelno = req.query['hn']

            myService.getYzm({
                hotelno:hotelno,
                userPhone:phone
            },function(err,body){
                if(err){
                    return reply({error:'请求验证码错误'})
                }
                reply('success')
            })

        }
    })

    server.route({
        method:'POST',path:'/my/commentList',
        handler:function(req,reply){
            var token = loginUtil.parseToken(req)
            if(token.error){
                return reply({
                    error:'获取评论列表失败',
                    reason:'尚未登录'
                })
            }
            var phone = token.phone,
                hotelno = token.hotelno;
            myService.getCommentList({
                phone:phone,
                hotelno:hotelno
            },function(err,body){
                if(err){
                    return reply({
                        error:'获取评论列表失败',
                        reason:err
                    })
                }
                reply(body.commentList);
            })
        }
    })
    server.route({
        method:'post',path:'/my/orderList',
        handler:function(req,reply){
            var token = loginUtil.parseToken(req)
            if(token.error){
                return reply({
                    error:'获取评论列表失败',
                    reason:'尚未登录'
                })
            }
            var phone = token.phone,
                hotelno = token.hotelno;
            myService.getOrderInfoList({
                phone:phone,
                hotelno:hotelno
            },function(err,body){
                if(err){
                    return reply({
                        error:'获取评论列表失败',
                        reason:err
                    })
                }
                console.log(body)
                //reply(body.commentList);
            })
        }
    })

    //0未评论,1已经评论

    server.route({
        method:'GET', path:"/my",
        handler: function (req, reply) {
            var hotelno = req.query['hn']
            //console.log(hotelno)
            var token = loginUtil.parseToken(req)
            //console.log(token)
            var phone = 'unknown',
                isLogin = false;
            if(!token.error){
                phone = token.phone
                isLogin = true
            }
            if(isLogin){
                async.parallel({
                    commentList:function(next){
                        myService.getCommentList({
                            phone:phone,
                            hotelno:hotelno
                        },function(err,body){
                            if(err){
                                return next(err)
                            }
                            next(null,body.commentList);
                        })
                    },
                    orderList:function(next){
                        myService.getOrderInfoList({
                            phone:phone,
                            hotelno:hotelno
                        },function(err,body){
                            if(err){
                                return next(err)
                            }
                            next(null,body.orderList);
                        })

                    }
                },function(err,results){
                    if(err){
                        return reply({
                            error:'获取您的信息失败',
                            reason:err
                        })
                    }
                    var list = results.orderList
                    var orderList =[],historyOrderList=[]

                    for(var i in list){
                        var order = list[i]
                        var code =  order.statusCode
                        if(code =='1' || code =='-1'){
                            orderList.push(order)
                        }else{
                            historyOrderList.push(order)
                        }
                    }

                    return reply.view("my", {
                        title: '预订',
                        active:{my:'active'},
                        hotelNo:hotelno,
                        bookingUrl:'/booking?hn='+hotelno,
                        hotelUrl:'/hotel?hn='+hotelno,
                        isLogin:isLogin,
                        phone:phone,
                        phoneDisplay:phone.substr(0,3)+'****'+phone.substr(7),
                        orderList:orderList,
                        historyOrderList:historyOrderList,
                        commentList:results.commentList
                    })
                })
            }else{
                return reply.view("my", {
                    title: '我的',
                    active:{my:'active'},
                    hotelNo:hotelno,
                    bookingUrl:'/booking?hn='+hotelno,
                    hotelUrl:'/hotel?hn='+hotelno,
                    isLogin:isLogin,
                    phone:phone,
                    //orderList:[],
                    //commentList:[]
                })
            }


        }
    })

    server.route({
        method:'post',path:'/commentOrder',
        handler:function(req,reply){
            var token = loginUtil.parseToken(req)
            //console.log(token)
            if(token.error){
               return reply({error:'您的登录状态非法,请先登录'})
            }
            var payload = req.payload;
            var orderNum = payload.orderNum,
                hotelno = payload.hotelno || req.query['hn'],
                evContent = payload.evContent,
                evClass ='1'
            myService.commentOrder({
                orderNum:orderNum,
                hotelno:hotelno,
                evContent:evContent,
                evclass:evClass,
                phone:token.phone
            },function(err,body){
                if(err){
                    return reply({
                        error:err,
                    })
                }
                reply('success')
            })
        }
    })
}
